<template>
	<view v-if="dataOk">
		<scroll-view scroll-y="true" class="scroll-height">
			<view class="topContent">
				<view class="title">
					<text>{{topTile}}</text>
					<text class="green">({{$t('inspection.offline.offline')}})</text>
				</view>
				<view class="time">
					{{$t('inspection.taskExecute.actualEndTime')}}
					<text class="blue">{{resultData.consumeTime}}</text>
					{{$t('baseCommon.time.hours')}}
				</view>
			</view>
			<view class="bottomCon">
				<view class="flexBox">
					<view class="one">
						<view class="num blue">{{resultData.areaCount}}</view>
						<view>{{$t('inspection.stat.totalArea')}}</view>
					</view>
					<view class="one noBorder" @click="toInfo">
						<view class="num orange">{{resultData.areaNoCheckCount}}</view>
						<view>{{$t('inspection.stat.uncheckedArea')}}</view>
					</view>
				</view>
				<view class="flexBox">
					<view class="one">
						<view class="num blue">{{resultData.planCheckCount}}</view>
						<view>{{$t('inspection.stat.totalInspection')}}</view>
					</view>
					<view class="one" @click="toInfo">
						<view class="num orange">{{resultData.noCheckCount}}</view>
						<view>{{$t('inspection.stat.missedInspection')}}</view>
					</view>
					<view class="one noBorder">
						<view class="num blue">{{resultData.exemptCheckCount}}</view>
						<view>{{$t('inspection.stat.exemptionInspection')}}</view>
					</view>
				</view>
				<view class="flexBox">
					<view class="one">
						<view class="num blue">{{resultData.normalCount}}</view>
						<view>{{$t('inspection.stat.normalInspection')}}</view>
					</view>
					<view class="one" @click="toInfo">
						<view class="num orange">{{resultData.exceptionCount}}</view>
						<view>{{$t('inspection.stat.exceptionInspection')}}</view>
					</view><view class="one" @click="toInfo">
						<view class="num orange">{{resultData.abnormalCount}}</view>
						<view>{{$t('inspection.stat.abnormalInspection')}}</view>
					</view>
					<view class="one noBorder">
						<view class="num blue">{{resultData.defectCount}}</view>
						<view>{{$t('inspection.stat.defectCount')}}</view>
					</view>
				</view>
			</view>
			<view class="summaryInput">
				<textarea class="usertextarea" v-model="finishReport" :disabled="!isEdit" auto-height 
					:placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('inspection.taskPlan.finishReport') })" />
			</view>

			<view v-if="isEdit" class="summaryFile">
				<attach-file :attachFile="attachFile" :canEdit="true" @fileSet="fileSet"></attach-file>
			</view>
			
			<view v-if="!isEdit && attachFile" class="summaryFile">
				<attach-file :attachFile="attachFile"></attach-file>
			</view>
			
			<view class="applyContent">
				<label class="applyText" @click="applyCheck">
					<checkbox class="applyBox" :checked="isCheked" :disabled="!isEdit || resultData.noCheckCount == 0" />
					{{ $t('inspection.taskExecute.isEfi') }}
				</label>
			</view>
			<view class="reasonInput">
				<textarea class="usertextarea" v-model="efiReason" auto-height :disabled="!isEdit || resultData.noCheckCount == 0" 
					:placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('inspection.taskExecute.efiReason') })" />
			</view>
		</scroll-view>
		
		<view class="submitBtn" v-if="isEdit">
			<button type="primary" @click="userSubmit">{{$t('baseCommon.buttons.submit')}}</button>
		</view>
	</view>
</template>

<script>
	import attachFile from '@/components/attach-file/attach-file-offline.vue'
	import * as sqliteUtil from '@/utils/sqlite.js'
	import * as sqliteInspect from '@/utils/business/inspectSqlite.js'
	import { notice, utilities, file } from '@/utils/common.js'

	export default {
		components: {
			attachFile
		},
		data() {
			return {
				dataOk: false,
				taskId: '',
				finishReport: '',
				topTile: '',
				isEdit: true,
				resultData: {},
				attachFile: '',
				isCheked: false,
				isEfi: 0,
				efiReason: ''
			}
		},
		onLoad(option) {
			this.taskId = option.taskId
			this.topTile = option.topTile
			this.isEdit = JSON.parse(option.isEdit)
			this.$nextTick(() => {
				this.loadData()
			})

		},
		methods: {
			async loadData() {
				// 获得计划检查项
				let planCheck = await sqliteUtil.selectTableInfo('m_ri_task_inspection', 'task_id', this.taskId)
				this.resultData.planCheckCount = planCheck.length
				
				// 获得未检项
				let noCheck = await sqliteUtil.selectTableInfo('m_ri_task_inspection', 'task_id', this.taskId, 'exec_status', 0)
				this.resultData.noCheckCount = noCheck.length
				
				// 获得正常项
				let normal = await sqliteUtil.selectTableInfo('m_ri_task_inspection', 'task_id', this.taskId, 'exec_result', 0)
				this.resultData.normalCount = normal.length
				
				// 获得异常项
				let exception = await sqliteUtil.selectTableInfo('m_ri_task_inspection', 'task_id', this.taskId, 'exec_result', 1)
				this.resultData.exceptionCount = exception.length
				
				// 获得异常情况数
				let abnormal = await sqliteUtil.selectTableInfo('m_ri_task_inspection_abnormal', 'task_id', this.taskId)
				this.resultData.abnormalCount = abnormal.length
				
				// 获得发现缺陷
				this.resultData.defectCount = 0
				utilities.formatHumpLineTransfer(abnormal)
				abnormal.forEach(item=>{
					if(item.dfNo != null && item.dfNo) {
						this.resultData.defectCount++
					}
				})
				
				// 获得免检项
				let exemptCheck = await sqliteUtil.selectTableInfo('m_ri_task_inspection', 'task_id', this.taskId, 'exec_status', 2)
				this.resultData.exemptCheckCount = exemptCheck.length
				
				// 获得耗时
				let tasks = await sqliteUtil.selectTableInfo('m_ri_task', 'task_id', this.taskId)
				utilities.formatHumpLineTransfer(tasks)
				let taskInfo = tasks[0]
				if(taskInfo != null && taskInfo.actualEndTime != null) {
					let d1 = new Date(taskInfo.actualEndTime)
					let d2 = new Date(taskInfo.actualStartTime)
					let diff = d1.getTime() - d2.getTime(); // 这样得到的差值是微秒级别
					let days = diff / (1000 * 60 * 60 * 24);
					let hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
					let minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
					let l = days * 24 + hours + (minutes / 60);
					this.resultData.consumeTime = parseFloat(l).toFixed(2)
				}
				
				// 获得区域总数
				let area = await sqliteInspect.getTaskAreaStat(this.taskId)
				this.resultData.areaCount = area == null ? 0 : area.length
				
				// 获得区域未到位数
				let areaNoCheck = await sqliteInspect.getTaskAreaStat(this.taskId, 0)
				this.resultData.areaNoCheckCount = areaNoCheck == null ? 0 : areaNoCheck.length
				
				// 获得巡检总结和附件
				this.finishReport = taskInfo.finishReport
				this.attachFile = taskInfo.attachFile
				
				// 获得巡检任务补充信息
				let adds = await sqliteUtil.selectTableInfo('m_ri_task_info', 'task_id', this.taskId)
				if(adds && adds.length > 0) {
					utilities.formatHumpLineTransfer(adds)
					let addInfo = adds[0]
					this.isEfi = addInfo.isEfi
					this.isCheked = !!this.isEfi
					this.efiReason = addInfo.efiReason
				}
				
				this.dataOk = true
			},
			userSubmit() {
				if(this.isEfi == 1 && !this.efiReason) {
					notice.toast(this.$t('baseCommon.message.pleaseEnter', { title: this.$t('inspection.taskExecute.efiReason') }))
					return
				}
				
				let data = [
					{
						key: "finish_report",
						value: this.finishReport,
					},
					{
						key: "actual_end_time",
						value: utilities.getNowDateTime(),
					},
					{
						key: "update_time",
						value: utilities.getNowDateTime(),
					},
					{
						key: "task_state",
						value: 3,
					},
					{
						key: "attach_file",
						value: this.attachFile,
					}
				]
				let condition = [
					{
						key: "task_id",
						value: this.taskId
					}
				]
				sqliteUtil.modifyTableInfo('m_ri_task', data, condition)
				
				// 向巡检任务补充信息表里添加数据
				if(this.isEfi == 1) {
					sqliteInspect.insertMRiTaskInfo({
						taskId: this.taskId,
						isEfi: this.isEfi,
						efiNum: this.resultData.noCheckCount,
						efiReason: this.efiReason,
						applyTime: utilities.getNowDateTime(),
						applyState: 0
					})
				}
				
				// utilities.backMore(0, 2)
				uni.redirectTo({
					url: '/pages/business/inspect/offline/index'
				})
			},
			applyCheck() {
				if(!this.isEdit || this.resultData.noCheckCount == 0) {
					return
				}
				this.isCheked = !this.isCheked
				this.isEfi = this.isCheked ? 1 : 0
			},
			toInfo() {
				uni.navigateBack()
			},
			fileSet(data) {
				this.attachFile = data
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F1F1F1;
	}

	.scroll-height {
		/* #ifdef APP-PLUS */
		height: calc(100vh - 65px);
		/* #endif*/
		/* #ifdef H5 */
		height: calc(100vh - 110px);
		/* #endif*/
	}

	.topContent {
		background: white;
		font-size: 14px;

		.title {
			padding: 12px 10px;
			color: #333;
		}
		
		.time {
			color: #666;
			margin: 0 10px;
			margin-bottom: 8px;
			padding-bottom: 12px;
		
			.blue {
				margin: 0 5px;
				font-size: 18px;
			}
		}
	}

	.bottomCon {
		background: white;
		margin-bottom: 8px;
	}

	.flexBox {
		display: flex;
		align-items: center;
		padding: 5px 0;
		border-bottom: 1rpx solid #eee;
	}

	.one {
		text-align: center;
		font-size: 12px;
		color: #999;
		padding-bottom: 5px;
		margin-top: 10px;
		border-right: 1px solid #ddd;
		width: 50%;
		margin-bottom: 4px;

		.num {
			font-size: 30px;
			font-weight: 500;
		}
	}

	.noBorder {
		border-right: none;
	}

	.blue {
		color: $uni-color-primary;
	}

	.orange {
		color: $uni-color-warning;
	}

	.summaryInput {
		padding: 12px;
		font-size: 12px;
		background: white;

		.usertextarea {
			width: calc(100% - 12px);
			min-height: 150px;
			border: 1px solid #eee;
			padding: 10px 5px;
		}
	}

	.summaryFile {
		margin-bottom: 10px;
	}

	.applyContent {
		background-color: #fff;
	}
	
	.applyText {
		display: flex;
		align-items: center;
		padding: 10px 12px;
		font-size: 15px;
	}
	
	.applyBox {
		transform: scale(0.7);
	}
	
	.reasonInput {
		padding: 0 12px;
		padding-bottom: 10px;
		font-size: 12px;
		background: white;
	
		.usertextarea {
			width: calc(100% - 12px);
			min-height: 100px;
			border: 1px solid #eee;
			padding: 10px 5px;
		}
	}
	
	.submitBtn {
		position: fixed;
		bottom: 10px;
		width: 90%;
		left: 5%;
		margin: 0 auto;
	
		button {
			background-color: #1E91E6;
		}
	}
	
	::v-deep .fileinfo {
		padding: 0 12px;
	}
	
	::v-deep .n-file-content {
		padding-bottom: 10px;
	}
	
	::v-deep .no-data {
		margin-bottom: 0;
	}
</style>